home *** CD-ROM | disk | FTP | other *** search
- Imports System.Text.RegularExpressions
- Imports System.ComponentModel
- Imports System.Reflection
-
- ' an improved TextBox control
-
- <DefaultEvent("InvalidKey")> _
- Public Class TextBoxEx
- Inherits System.Windows.Forms.TextBox
-
- Event InvalidKey(ByVal sender As Object, ByVal e As EventArgs)
-
- Sub New()
- MyBase.New()
- End Sub
-
- '-------------------------------------------------------------------
- ' True if the field is required
- '-------------------------------------------------------------------
-
- <Description("True if the field is required and must contain a value"), _
- Category("Validation")> _
- Dim m_IsRequired As Boolean
-
- Property IsRequired() As Boolean
- Get
- Return m_IsRequired
- End Get
- Set(ByVal Value As Boolean)
- m_IsRequired = Value
- End Set
- End Property
-
- '-------------------------------------------------------------------
- ' The text of the message
- '-------------------------------------------------------------------
-
- Dim m_ErrorMessage As String
-
- <Description("The error message displayed in case of error"), _
- Category("Validation")> _
- Property ErrorMessage() As String
- Get
- Return m_ErrorMessage
- End Get
- Set(ByVal Value As String)
- m_ErrorMessage = Value
- End Set
- End Property
-
- '-------------------------------------------------------------------
- ' The BeepOnError property (defaults to True)
- '-------------------------------------------------------------------
-
- Dim m_BeepOnError As Boolean = True
-
- <Description("If True, a beep will be emitted if validation fails"), _
- DefaultValue(True)> _
- Property BeepOnError() As Boolean
- Get
- Return m_BeepOnError
- End Get
- Set(ByVal Value As Boolean)
- m_BeepOnError = Value
- End Set
- End Property
-
- '-------------------------------------------------------------------
- ' The ForeColor of the message
- '-------------------------------------------------------------------
-
- Dim m_ErrroForeColor As Color = SystemColors.ControlText
-
- <Description("The ForeColor of the DisplayControl in case of error"), _
- Category("Validation")> _
- Property ErrorForeColor() As Color
- Get
- Return m_ErrroForeColor
- End Get
- Set(ByVal Value As Color)
- m_ErrroForeColor = Value
- End Set
- End Property
-
- Sub ResetErrorForeColor()
- ErrorForeColor = SystemColors.ControlText
- End Sub
-
- Function ShouldSerializeErrorForeColor() As Boolean
- Debug.WriteLine("ShouldSerializeErrorForeColor for " & Me.Name)
- Return Not Me.ErrorForeColor.Equals(SystemColors.ControlText)
- End Function
-
- '-------------------------------------------------------------------
- ' The ValidateRegex property
- '-------------------------------------------------------------------
-
- <Description("The regular expression used to validate the control on exit"), _
- Category("Validation")> _
- Property ValidateRegex() As String
- Get
- Return m_ValidateRegex
- End Get
- Set(ByVal Value As String)
- ' Before setting this mask, which check that its a valid regular expression
- Try
- ' We attempt to use this mask as a regular expression
- If Value <> "" Then
- Dim dummy As Boolean = Regex.IsMatch("abcde", Value)
- End If
- ' if no error assignment is ok
- m_ValidateRegex = Value
- Catch ex As Exception
- MessageBox.Show(ex.Message, "Invalid Property", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Set
- End Property
-
- Dim m_ValidateRegex As String
-
- '-------------------------------------------------------------------
- ' The optional control that displays the error message
- '-------------------------------------------------------------------
-
- Dim m_DisplayControl As Control
-
- <Description("The control that will display the error message"), _
- Category("Validation")> _
- Property DisplayControl() As Control
- Get
- Return m_DisplayControl
- End Get
- Set(ByVal Value As Control)
- m_DisplayControl = Value
- End Set
- End Property
-
- '-------------------------------------------------------------------
- ' The type of value the control must contain
- '-------------------------------------------------------------------
-
- Enum ValidTypes
- Any = 0
- [Byte]
- [Short]
- [Integer]
- [Long]
- [Single]
- [Double]
- [Decimal]
- [DateTime]
- End Enum
-
- Dim m_ValidType As ValidTypes
-
- <Description("The type of value that is valid for this field"), _
- Category("Validation"), DefaultValue(ValidTypes.Any)> _
- Property ValidType() As ValidTypes
- Get
- Return m_ValidType
- End Get
- Set(ByVal Value As ValidTypes)
- m_ValidType = Value
- End Set
- End Property
-
- '-------------------------------------------------------------------
- ' The IsValid property
- '-------------------------------------------------------------------
-
- ' This property has the same semantics as the Validate method.
- ' (Just an example to show how you can hide a property.)
-
- <Browsable(False)> _
- ReadOnly Property IsValid() As Boolean
- Get
- Return Validate()
- End Get
- End Property
-
- '-------------------------------------------------------------------
- ' The Validate method
- '-------------------------------------------------------------------
-
- <Description("Returns True if the current value passes the validation test")> _
- Function Validate(Optional ByVal DisplayMessage As Boolean = True) As Boolean
- ' Assume control passed the validation.
- Validate = True
-
- ' Apply the IsRequired property.
- If Me.IsRequired And Me.Text = "" Then
- Validate = False
- End If
-
- ' Apply the ValidType property.
- If Validate = True And Me.Text <> "" Then
- Validate = CheckValueType(Me.Text)
- End If
-
- ' Apply the ValidateRegex property.
- If Validate = True And Me.ValidateRegex <> "" Then
- Validate = Regex.IsMatch(Me.Text, Me.ValidateRegex)
- End If
-
- ' If the validation failed, the called asked to see a message box,
- ' the client defined a display control and an error message, then show the message on the control.
- If DisplayMessage And Not (DisplayControl Is Nothing) And Me.ErrorMessage <> "" Then
- If Validate Then
- ' Delete and previous error message.
- DisplayControl.Text = ""
- Else
- ' Display error message, and enforce colors.
- DisplayControl.Text = Me.ErrorMessage
- DisplayControl.ForeColor = m_ErrroForeColor
- End If
- End If
-
- ' emit a beep in case of error, if so requested
- If Not Validate And Me.BeepOnError Then Beep()
- End Function
-
- ' Check that a value can be assigned to the value type
- Function CheckValueType(ByVal o As Object) As Boolean
- Dim res As Object
- Try
- Select Case m_ValidType
- Case ValidTypes.Byte : res = CByte(o)
- Case ValidTypes.Short : res = CShort(o)
- Case ValidTypes.Integer : res = CInt(o)
- Case ValidTypes.Long : res = CLng(o)
- Case ValidTypes.Single : res = CSng(o)
- Case ValidTypes.Double : res = CDbl(o)
- Case ValidTypes.Integer : res = CDec(o)
- Case ValidTypes.DateTime : res = CDate(o)
- End Select
- ' if we get here, the assignment was ok
- Return True
- Catch
- ' else there is an error
- Return False
- End Try
- End Function
-
- ' this method is called when the control is about to raise the Validating event.
-
- Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
- If Me.Validate() Then
- ' If validation is ok, let the base class fire the Validating event.
- MyBase.OnValidating(e)
- Else
- ' else, cancel the focus shift.
- e.Cancel = True
- End If
- End Sub
-
- End Class
-